#!/bin/bash

# iptables -t nat -A OUTPUT -d 192.168.0.22 -p tcp --dport 6443 -j DNAT --to-destination 172.20.150.48:6443
# 授权数据 资源文件路径 执行的操作

# JIANMU_COMMAND
# JIANMU_RESOURCE_FILE_PATH
# JIANMU_CERTIFICATE_AUTHORITY_DATA
# JIANMU_SERVER
# JIANMU_CLIENT_CERTIFICATE_DATA
# JIANMU_CLIENT_KEY_DATA
set -e

ls -l

generate_certificate() {
  python ./build_configuration.py
  echo "build coscli config complete"
}


upload_resources() {
  # 进入项目代码目录
  cd $JIANMU_GIT_DIR
  pwd
  ls -l
  # cos上传
  oss_path="cos://jryg-ai-model-1257379299/model/${JIANMU_NAMESPACE}/${JIANMU_JOB_NAME}/c${JIANMU_CHANNEL_ID}_t${JIANMU_TAG_ID}_v${JIANMU_VERSION_ID}_${JIANMU_MT_ID}/code"
  echo "oss path is: ${oss_path}"
  ls -l /usr/local/bin/coscli
  /usr/local/bin/coscli config show
  echo "push cos start"
  /usr/local/bin/coscli sync ${JIANMU_GIT_DIR}/ ${oss_path}/ --exclude=".git" -r
  /usr/local/bin/coscli cp ${JIANMU_GIT_DIR}/env/${JIANMU_JOB_NAME}/.env.${JIANMU_NAMESPACE} ${oss_path}/.env
  echo "cos push complete"

  dockerfile="${JIANMU_DOCKER_FILE}/${JIANMU_JOB_NAME}.Dockerfile"

  sed -i "/ENTRYPOINT/i ENV CHANNEL_ID $JIANMU_CHANNEL_ID" $dockerfile
  sed -i "/ENTRYPOINT/i ENV TAG_ID $JIANMU_TAG_ID" $dockerfile
  sed -i "/ENTRYPOINT/i ENV VERSION_ID $JIANMU_VERSION_ID" $dockerfile
  sed -i "/ENTRYPOINT/i ENV MT_ID $JIANMU_MT_ID" $dockerfile
  echo "dockerfile model param sed complete"
}


generate_certificate
upload_resources
